package server

import (
	"3-19yk/common/models/mysql"
	"3-19yk/common/proto/user"
	"3-19yk/pkg"
	"fmt"
	"github.com/google/uuid"
	"gorm.io/gorm"
	"strconv"
)

func OrderAdd(req *user.OrderAddReq) (*user.OrderAddResp, error) {
	scenic := mysql.Scenic{}
	show, err := scenic.ScenicShow(int(req.ScenicId))
	if err != nil {
		return nil, fmt.Errorf("查询失败")
	}
	newString := uuid.NewString()
	order := mysql.Order{
		Model:         gorm.Model{},
		UserId:        int(req.UserId),
		ScenicId:      int(req.ScenicId),
		RealNameId:    int(req.RealNameId),
		OrderSn:       newString,
		ScenicName:    show.ScenicName,
		ScenicPrice:   show.ScenicPrice,
		ScenicAddress: show.ScenicAddress,
		AdmNum:        int(req.AmdNum),
		AdmPeopleNum:  int(req.AdmPeopleNum),
		SelectData:    int(req.SelectData),
		AdmStatus:     int(req.AdmStatus),
		OrderStatus:   1,
		InCures:       1,
	}
	if req.AmdNum != req.AdmPeopleNum {
		return nil, fmt.Errorf("购票人数和票数必须一致")
	}
	if show.Num <= 0 {
		return nil, fmt.Errorf("库存不足")
	}
	if int(req.RealNameId) != 4 {
		return nil, fmt.Errorf("购票人信息不存在")
	}
	err = order.Create(&order)
	if err != nil {
		return nil, fmt.Errorf("订单创建失败")
	}
	float := strconv.FormatFloat(show.ScenicPrice, 'f', 2, 64)
	pay := pkg.NewAliPay()
	pays := pay.Pays(show.ScenicName, newString, float)
	return &user.OrderAddResp{
		Key:     pays,
		OrderSn: newString,
	}, nil
}

func OrderCall(req *user.OrderCallReq) (*user.OrderCallResp, error) {
	fmt.Println("支付宝回调")
	fmt.Println(req.OrderSn)
	fmt.Println(req.OrderStatus)
	var status int
	fmt.Println("8988888888888888")
	if req.OrderStatus == "WAIT_BUYER_PAY" {
		status = 1
	} else if req.OrderStatus == "TRADE_CLOSED" {
		status = 2
	} else if req.OrderStatus == "TRADE_SUCCESS" {
		status = 3
	} else if req.OrderStatus == "TRADE_FINISHED" {
		status = 4
	}

	order := mysql.Order{}
	err := mysql.DB.Debug().Model(&order).Where("order_sn", req.OrderSn).Update("order_status", status).Error
	if err != nil {
		return nil, fmt.Errorf("支付宝回调失败")
	}
	return &user.OrderCallResp{Success: "支付宝回调成功"}, nil
}

func OrderShow(req *user.OrderShowReq) (*user.OrderShowResp, error) {
	order := mysql.Order{}
	show, err := order.OrderShow(int(req.Id))
	if err != nil {
		return nil, fmt.Errorf("查询失败")
	}
	return &user.OrderShowResp{
		UserId:        int64(show.UserId),
		ScenicId:      int64(show.ScenicId),
		RealNameId:    int64(show.RealNameId),
		AmdNum:        int64(show.AdmNum),
		AdmPeopleNum:  int64(show.AdmPeopleNum),
		SelectData:    int64(show.SelectData),
		AdmStatus:     int64(show.AdmStatus),
		OrderSn:       show.OrderSn,
		ScenicName:    show.ScenicName,
		ScenicPrice:   float32(show.ScenicPrice),
		ScenicAddress: show.ScenicAddress,
		OrderStatus:   int64(show.OrderStatus),
		InCures:       int64(show.InCures),
	}, nil
}

func InCure(req *user.InCureReq) (*user.InCureResp, error) {
	cure := mysql.InCure{
		Model:    gorm.Model{},
		UserId:   int(req.UserId),
		ScenicId: int(req.ScenicId),
		OrderId:  int(req.OrderId),
	}
	err := cure.Create(&cure)
	if err != nil {
		return nil, fmt.Errorf("投保失败")
	}
	mysql.DB.Debug().Where("id = ?", req.OrderId).Find(&mysql.Order{})
	order := []mysql.Order{}
	for _, i2 := range order {
		i2.InCures = 2
		err := mysql.DB.Save(&order).Error
		if err != nil {
			return nil, fmt.Errorf("投保失败")
		} else {
			return nil, fmt.Errorf("投保成功")
		}
	}
	return &user.InCureResp{Success: "投保成功"}, nil
}
